/* global $ */
/* global App */
var loginer = App.get("loginer");
// 获取所有课程列表
$.ajax({
	url: "/api/complex/StudentCourse",
	type: "get",
	data: {
		limit: 30
	},
	success: function (result) {
		console.log(result);
		App.set("$Cache_all_course_list", result);
	},
	error: function (e) {
		console.log(e);
	}
});
// 获取自己的课程列表
$.ajax({
	url: "/api/complex/MyCourse",
	type: "get",
	data: {
		sid: loginer.sid
	},
	success: function (result) {
		console.log(result);
		App.set("$Cache_my_all_course_list", result);
	}
});
// 获取选定课程的详细内容,未添加
App.set("$Event.get_local_course_detail", function (e, vi) {
	var cid = vi.get("cid");
	var coursetype = vi.get("coursetype");
	$.ajax({
		url: "/api/complex/StudentCourse",
		type: "get",
		data: {
			cid: cid,
			coursetype: coursetype,
		},
		success: function (result) {
			console.log(result);
			App.set("$Cache_show_local_course_detail", true);
			App.set("$Cache_local_course_name", vi.get("coursename"));
			App.set("$Cache_local_course_type", coursetype);
			App.set("$Cache_local_course_cid", cid);
			App.set("$Cache_local_course_tid", vi.get("tid"));
			App.set("$Cache_local_course_tname", vi.get("tname"));
			App.set("$Cache_local_course_detail", result);
		},
		error: function (e) {
			if (e.status == "404") {
				alert("此课程还没有任何内容");
			}
		}
	});
});
// 获取选定课程的详细内容,已添加
App.set("$Event.get_my_local_course_detail", function (e, vi) {
	var cid = vi.get("cid");
	var coursetype = vi.get("ctype");
	console.log(cid, coursetype);
	$.ajax({
		url: "/api/complex/StudentCourse",
		type: "get",
		data: {
			cid: cid,
			coursetype: coursetype
		},
		success: function (result) {
			console.log(result);
			App.set("$Cache_show_my_local_course_detail", true);
			App.set("$Cache_my_local_course_name", vi.get("cname"));
			App.set("$Cache_my_local_course_type", coursetype);
			App.set("$Cache_my_local_course_cid", cid);
			App.set("$Cache_my_local_course_tid", vi.get("tid"));
			App.set("$Cache_my_local_course_tname", vi.get("tname"));
			App.set("$Cache_my_local_course_detail", result);
		},
		error: function (e) {
			console.log(e);
		}
	});
});
// 关闭课程详细列表,未添加
App.set("$Event.close_local_course_detail", function () {
	App.set("$Cache_show_local_course_detail", false);
});
// 关闭课程详细列表,已添加
App.set("$Event.close_my_local_course_detail", function () {
	App.set("$Cache_show_my_local_course_detail", false);
});
// 给自己添加新的课程
App.set("$Event.add_student_new_course", function (e, vi) {
	var $Cache_my_all_course_list = App.get("$Cache_my_all_course_list");
	var cid = App.get("$Cache_local_course_cid");
	for (var i = 0; i < $Cache_my_all_course_list.length; i++) {
		if ($Cache_my_all_course_list[i].cid == cid) {
			alert("您已添加过此课程");
			return;
		};
	};
	myConfirm("确定添加此课程吗？？？", function () {
		var data = {
			sid: loginer.sid,
			tid: App.get("$Cache_local_course_tid"),
			tname: App.get("$Cache_local_course_tname"),
			cid: App.get("$Cache_local_course_cid"),
			cname: App.get("$Cache_local_course_name"),
			ctype: App.get("$Cache_local_course_type")
		};
		if (data.ctype == 1) {
			var course = App.get("$Cache_local_course_detail");
			for (var i = 0; i < course.length; i++) {
				course[i].id = (new Date(course[i].start)).getTime();
				course[i].sid = loginer.sid;
				course[i].end = " " + "24:00:00";
				course[i].tid = '';
			}
		};
		$.ajax({
			url: "/api/complex/StudentCourse",
			type: "post",
			data: { data: data, course: course },
			success: function (result) {
				console.log(result);
				$Cache_my_all_course_list.unshift(data);
				App.set("$Cache_my_all_course_list", $Cache_my_all_course_list);
			},
			error: function (e) {
				console.log(e);
			}
		});
		console.log(data);
	});
});
// 删除已添加的课程
App.set("$Event.remove_course", function (e, vi) {
	myConfirm("确定要删除此课程吗？？？", function () {
		var data = {
			sid: loginer.sid,
			cid: vi.get("cid"),
			coursetype: vi.get("ctype")
		};
		//		console.log(data)
		$.ajax({
			url: "api/complex/StudentCourse",
			type: "delete",
			data: data,
			success: function (result) {
				vi.remove();
				console.log(result);
				alert("删除成功！！！");
			},
			error: function (e) {
				console.log(e);
			}
		});
	});
});